﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace publicLibrary
{
    public class resource
    {
        string resourceFile = AppDomain.CurrentDomain.BaseDirectory + "App_Data/resource.xml";
        XElement resourceXml;

        public resource()
        {
           resourceXml = XElement.Load(resourceFile);
        }

        public XElement Address()
        {
            return resourceXml.Element("area");
        }

        //省
        public XElement province(string id)
        {
            id = id ?? "000000";
            id = id.Substring(0, 2).PadRight(6, '0');
            var province = Address().Elements("province").Where(d => d.Attribute("provinceID").Value == id);
            if (!province.Any()) return null;
            return province.First();
        }
        public string getProvince(string id)
        {
            //return province(id).Attribute("province").Value.TrimEnd('省');
            return province(id).Attribute("province").Value;
        }
        //市
        public XElement city(string id)
        {
            id = id.Substring(0, 4).PadRight(6, '0');
            return province(id).Elements("City").Where(d => d.Attribute("CityID").Value == id).First();
        }
        public string getCity(string id)
        {
            //return city(id).Attribute("City").Value.TrimEnd('市');
            return city(id).Attribute("City").Value;
        }




    }
}